<template>
  <view class="forgot-pwd">
    <!-- #ifdef H5 -->
    <van-nav-bar @click-left="onClickLeft" left-arrow></van-nav-bar>
    <!-- #endif -->
    <view class="title text-bold">重置密码</view>
    <view class="form-wrap form-forgot">
      <view class="input-wrap">
        <input
          placeholder-class="placeholder"
          v-model="form.account"
          placeholder="请输入账号"
          maxlength="30" />
      </view>
      <view class="input-wrap">
        <input
          class="input-code"
          v-model="form.code"
          placeholder-class="placeholder"
          placeholder="请输入短信验证码"
          maxlength="10" />
        <button
          class="bnt-code"
          :class="{ 'bnt-disabled': codeObj.disabled }"
          :plain="true"
          :disabled="codeObj.disabled"
          @tap="tapCode">
          {{ codeObj.codeName }}
        </button>
      </view>
      <view class="input-wrap">
        <input
          class="input-wrap"
          v-model="form.password"
          :password="isPwd"
          placeholder-class="placeholder"
          placeholder="请输入新密码"
          maxlength="30" />
        <view
          class="iconfont icon-pwd"
          :class="[isPwd ? 'icon-pwd-close' : 'icon-pwd-open']"
          @tap="tapPwd"></view>
      </view>
    </view>
    <view class="tip">注：接收验证码手机为企业账号所绑定的手机！</view>
    <button class="bnt-wrap bnt-submit" @tap="tapSubmit">确认</button>
  </view>
</template>

<script lang="ts" setup>
import { setToastInstance, systemInfo } from '@/mixins/info';
import { frontUpdatePwd, sendMobileVCode } from '@/service/apis/login';
import { getMd5 } from '@/utils/util';
// #ifdef MP-WEIXIN
import { useToast } from '@/components/wxToast';
setToastInstance(useToast());
// #endif

// 分享
onShareAppMessage(() => {
  return {
    title: '',
    path: '/pages/stationMap/stationMap',
  };
});

const $toast: any = inject('$Toast');

let form = reactive({
  account: '',
  code: '',
  password: '',
});

// 验证码
let codeObj = reactive({
  codeName: '获取验证码',
  disabled: false,
  count: 120,
  timeout: 0,
});
const tapCode = () => {
  sendMobileVCode({ accountNumber: form.account }).then((res: any) => {
    $toast('验证码发送成功');
    codeObj.timeout = setInterval(() => {
      if (codeObj.count < 1) {
        codeObj.disabled = false;
        codeObj.codeName = '重新获取';
        codeObj.count = 120;
        clearInterval(codeObj.timeout);
      } else {
        codeObj.disabled = true;
        codeObj.codeName = `${codeObj.count--}s后重发`;
      }
    }, 1000);
  });
};

// 密码切换
let isPwd = ref(true);
const tapPwd = () => {
  isPwd.value = !isPwd.value;
};

// 确认
const tapSubmit = () => {
  if (!form.account) {
    $toast('请输入账号');
    return;
  }
  if (!new RegExp(/^(?!^\d+$)[0-9a-zA-Z_]*$/).test(form.account)) {
    $toast('账号只能输入字母或字母+数字');
    return;
  }
  if (!form.code) {
    $toast('请输入验证码');
    return;
  }
  if (!form.password) {
    $toast('请输入密码');
    return;
  }
  frontUpdatePwd({
    accountNumber: form.account,
    verificationCode: form.code,
    password: getMd5(form.password),
  }).then((res: any) => {
    uni.redirectTo({
      url: '/pages/login/login',
    });
  });
};
// #ifdef H5
const onClickLeft = () => {
  uni.navigateBack();
};
setTimeout(() => {
  const navBarEle = document.querySelector('.van-nav-bar') as HTMLElement;
  navBarEle.style.paddingTop = `${systemInfo.statusBarHeight}px`;
});
// #endif
</script>

<style lang="scss" scoped>
@import './login.scss';
.forgot-pwd {
  .title {
    margin: 32px;
    line-height: 30px;
    font-size: 20px;
    letter-spacing: 4.5px;
  }
  .form-forgot {
    margin: 28px 32px 0 32px;
  }

  .tip {
    margin: 15px 32px 26px 32px;
    line-height: 24px;
    font-size: 14px;
    color: #666;
  }
  .bnt-submit {
    width: 311px;
    height: 44px;
    border-radius: 100px;
  }
}
</style>